<?php

namespace App\Http\Controllers\Saas;

use App\Http\Controllers\Controller;
use App\Model\QA;
use App\Utils\Result;
use Illuminate\Http\Request;

class QAController extends Controller
{
    public function index(Request $request)
    {
        $type = $request->input('type');
        $query = QA::query();

        if ($type) {
            $query->where('type', $type);
        }

        $data = $query->orderByDesc('sortable')->get(['id', 'title', 'sortable', 'type']);

        return Result::success($data);
    }

    public function detail($id)
    {
        $data = QA::find($id);

        return Result::success($data);
    }

    public function create(Request $request)
    {
        $data = $request->only([
            'title',
            'content',
            'sortable',
            'type'
        ]);
        QA::create($data);

        return Result::success();
    }

    public function update(Request $request, $id)
    {
        $data = $request->only([
            'title',
            'content',
            'sortable',
            'type'
        ]);
        $qa = QA::query()->find($id);
        $qa->fill($data);
        $qa->save();

        return Result::success();
    }

    public function destroy($id)
    {
        QA::query()->where('id', $id)->delete();

        return Result::success();
    }
}
